package com.mud.dnd.frontend.rs;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.ejb.CardManager;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.frontend.rs.common.RestInputConverter;



@Stateless
@Path("/Card")
public class CardJSONService 
{

	
	@EJB
	CardManager ejbService;
	
	@GET
	@Path("/query")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Card> getCards(@QueryParam("id") Integer id,@QueryParam("name") String name,@QueryParam("desc") String description,@QueryParam("deckID") Integer deckID, @QueryParam("picsURL")String pictureURL,
			  @QueryParam("type")String type,@QueryParam("userName")String userName,@QueryParam("rate") Double rate,@QueryParam("rateOp") String rateOp, @QueryParam("rarity")Integer rarity, @QueryParam("rarityOp")String rarityOp) {
		
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try{
		
		
		if(id!=null)
		{
			params.add(new QueryParameter("id", QueryParamOperator.EQUALS, id));
		}
		if(name!=null)
		{
			params.add(new QueryParameter("name", QueryParamOperator.LIKE, name));
		}
		if(description!=null)
		{
			params.add(new QueryParameter("description", QueryParamOperator.LIKE, description));
		}
		if(pictureURL!=null)
		{
			params.add(new QueryParameter("pictureURL", QueryParamOperator.LIKE, pictureURL));
		}
		if(type!=null)
		{
			params.add(new QueryParameter("type", QueryParamOperator.LIKE, type));
		}
		if(deckID!=null)
		{
			params.add(new QueryParameter("deckID", QueryParamOperator.EQUALS, deckID));
		}
		if(userName!=null)
		{
			params.add(new QueryParameter("author", QueryParamOperator.LIKE, userName));
		}
		if(rarity!=null )
		{
			
			params.add(new QueryParameter("rarity", RestInputConverter.queryParamOperatorConverter(rarityOp), rarity));
		}
		if(rate!=null)
		{
			
			params.add(new QueryParameter("rate", RestInputConverter.queryParamOperatorConverter(rateOp), rate));
		}
		}catch(DAOException e)
		{
			return null;
		}
		

 
		return ejbService.getCards(params); 
 
	}
	
	@GET
	@Path("/create")
	@Produces(MediaType.APPLICATION_JSON)
	public String createCard(@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,
			  @QueryParam("type")String type, @QueryParam("rarity")int rarity,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		
		int resultID;
		
		try {
			resultID = ejbService.createCard(name, description, pictureURL, type,rarity, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
 
		return "OK The new card ID:"+String.valueOf(resultID); 
 
	}
	
	@GET
	@Path("/addToDeck")
	@Produces(MediaType.APPLICATION_JSON)
	public String addCardToDeck(@QueryParam("cardID") int cardID,@QueryParam("deckID") int deckID,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID)
	{
		try {
			ejbService.addCardtoDeck(cardID, deckID, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	@GET
	@Path("/update")
	@Produces(MediaType.APPLICATION_JSON)
	public String updateCard(@QueryParam("id") int id,@QueryParam("name") String name,@QueryParam("desc") String description, @QueryParam("picsURL")String pictureURL,
			  @QueryParam("type")String type,@QueryParam("rate") int rate, @QueryParam("rarity")int rarity,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		
		
		try {
			ejbService.updateCard(id,name, description, pictureURL, type,rate,rarity, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 

	}
	
	@GET
	@Path("/delete")
	@Produces(MediaType.APPLICATION_JSON)
	public String deleteCard(@QueryParam("id") int id,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID) {
		try {
			ejbService.deleteCard(id, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 
	}
	
	@GET
	@Path("/removeFromDeck")
	@Produces(MediaType.APPLICATION_JSON)
	public String removeCardFromDeck(@QueryParam("cardID") int cardID,@QueryParam("deckID") int deckID,@QueryParam("userName")String userName,@QueryParam("sessionID")String sessionID)
	{
		try {
			ejbService.removeCardfromDeck(cardID, deckID, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	
	
}
